import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/**
 * @date 2022/9/14 12:59
 * @author: Akie
 * @description: 输入输出界面

 */


public class Ui {

    public static void main(String[] args) {
        Ui ui = new Ui();
        ui.enterMainMenu();


    }
    public void enterMainMenu() {
        boolean isFlag = true;
        do {
            System.out.println("*******食堂选餐系统*******");
            System.out.println();
            System.out.println("\t" + "1 单 次 选 餐");
            System.out.println("\t" + "2 全 天 选 餐");
            System.out.println("\t" + "3 退      出");
            System.out.println();
            System.out.println("\t" + "请选择（1-3）：" );

            int menu = CMUtility.readMenuSelection();
            switch (menu) {
                case '1': OneChanceMenu();
                    break;
                case '2': new AllDay().AllDayMenu();
                    break;
                default :
                {
                    System.out.print("确认是否退出(Y/N)：");
                    char exit = CMUtility.readConfirmSelection();
                    if (exit == 'Y') {
                        isFlag = false;
                    }
                }
            }
        } while (isFlag);

    }

    public void OneChanceMenu() {
        boolean isFlag = true;
        do {
            System.out.println("*******食堂选餐系统*******");
            System.out.println();
            System.out.println("\t" + "1 随 机 选 餐");
            System.out.println("\t" + "2 手 动 选 餐");
            System.out.println("\t" + "3 按 心 情 来");
            System.out.println("\t" + "4 退      出");
            System.out.println();
            System.out.println("\t" + "请选择（1-4）：" );

            int menu = CMUtility.readMenuSelection();
            switch (menu) {
                case '1': ChooseMallRandom();
                    break;
                case '2': ChooseMallByYourself();
                    break;
                case '3': ChooseMallByYourMood();
                    break;
                default :
                {
                    System.out.print("确认是否退出(Y/N)：");
                    char exit = CMUtility.readConfirmSelection();
                    if (exit == 'Y') {
                        isFlag = false;
                    }
                }
            }
        } while (isFlag);

    }

    public void SelectOrRandom(){
        System.out.println("今天想随机还是自选：（随机：Y, 自选：N）");
        Boolean isFlag = true;
        do {
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next().toUpperCase();
            if (input.equals("Y")) {
                ChooseMallRandom();
                isFlag = false;
            } else if (input.equals("N")) {
                ChooseMallByYourself();
                isFlag = false;
            } else {
                System.out.println("你的输入有误，请重新输入！");
            }
        } while (isFlag);
    }

    public void ChooseMallRandom(){
        Boolean isFlag = true;;
        do {
            System.out.println("---------------------开始选择---------------------");
            int num = ChooseMall();
            String meal =ChooseMeal(num);
            System.out.println("系统会为你随机选择现在吃什么！" + "今天选择的是第" + num + "食堂");
            System.out.println("今天选择:" + "\t" + meal);
            System.out.println("请输入：（Y/N）");
            Scanner scanner = new Scanner(System.in);
            String input = scanner.next().toUpperCase();
            if (input.equals("Y")) {
                System.out.println("今天就决定是它了！" + "\t" + meal);
                System.out.println("---------------------选择完成---------------------");
                isFlag = false;
            } else if (input.equals("N")) {
                System.out.println("系统将会重新选择。");
            } else {
                System.out.println("你的输入有误，请重新输入！");
            }
        } while (isFlag);
    }

    public void ChooseMallByYourself(){
        Boolean isFlag = true;
        Boolean isFlag1 = true;
        loop1:do {
            System.out.println("---------------------开始选择---------------------");
            System.out.println("请选择你自己想吃的食堂：（1,2,3,6,7,8=后街）");
            Scanner scanner = new Scanner(System.in);
            int input = scanner.nextInt();
            if (input == 1 || input == 2 || input == 3 || input == 6 || input == 7 || input == 8) {
                int loop = 0;
                if (input == 1 || input == 2 || input == 3 || input == 6 || input == 7) {
                    System.out.println("今天选择的是第" + input + "食堂");
                }else {
                    System.out.println("今天选择的是后街");
                }
                do {
                    String meal = ChooseMeal(input);
//                    ChooseMealAllDayRandom();
                    System.out.println("今天选择:" + "\t" + meal);
                    System.out.println("请输入：（Y/N）");
                    Scanner scanner1 = new Scanner(System.in);
                    String input1 = scanner.next().toUpperCase();
                    if (input1.equals("Y")) {
                        System.out.println("今天就决定是它了！" + "\t" + meal);
                        System.out.println("---------------------选择完成---------------------");
                        isFlag1 = false;
                    } else if (input1.equals("N")) {
                        loop +=1;
                        if (loop >= 4) {
                            System.out.println("是否选择其他食堂？请输入：（Y/N）");
                            String other = scanner.next().toUpperCase();
                            if (other.equals("Y")) {
                                continue loop1;
                            } else if (other.equals("N")) {
                                loop = 0;
                            }else {
                                System.out.println("你的输入有误，请重新输入！");
                            }
                        }
                        System.out.println("系统将会重新选择。");
                    } else {
                        System.out.println("你的输入有误，请重新输入！");
                    }
                } while (isFlag1);
                isFlag = false;
            }else {
                System.out.println("您的输入有误！");
            }
        }while (isFlag);
    }

    public void ChooseMallByYourMood(){
        boolean isFlag = true;
        loop: do {
            System.out.println("---------------------开始选择---------------------");
            System.out.println();
            System.out.println("\t\t" + "1 吃  面");
            System.out.println("\t\t" + "2 吃  饭");
            System.out.println("\t\t" + "3 吃  饼");
            System.out.println();
            System.out.println("\t\t" + "请选择（1-3）：" );

            int menu = CMUtility.readMenuSelection();
            switch (menu) {
                case '1':
                    ChooseNoodle();
                    break loop;
                case '2':
                    ChooseRice();
                    break loop;
                case '3':
                    ChoosePie();
                    break loop;
                default :
                {
                    System.out.print("确认是否退出(Y/N)：");
                    char exit = CMUtility.readConfirmSelection();
                    if (exit == 'Y') {
                        isFlag = false;
                    }
                }
            }
        } while (isFlag);
    }

    public int ChooseMall() {
        OneAndTwoMall oneAndTwoMall = new OneAndTwoMall();
        ThreeMall threeMall = new ThreeMall();
        SixAndSevenMall sixAndSevenMall = new SixAndSevenMall();
        int[] numStr = {1, 3, 6,8};
        int i = (int) (Math.random() * 4);
        if (i == 4) {
            i = 3;
        }
        int num = numStr[i];
        switch (num) {
            case 1:
                num = oneAndTwoMall.getMall();
                break;
            case 3:
                num = 3;
                break;
            case 6:
                num = sixAndSevenMall.getMall();
                break;
            case 8:
                num = 8;
                break;
        }

        return num;
    }

    public String ChooseMeal(int num){
        OneAndTwoMall oneAndTwoMall = new OneAndTwoMall();
        ThreeMall threeMall = new ThreeMall();
        SixAndSevenMall sixAndSevenMall = new SixAndSevenMall();
        BackStreet backStreet = new BackStreet();
        String meal = null;
        switch (num) {
            case 1:
                meal = oneAndTwoMall.getMeal1();
                break;
            case 2:
                meal = oneAndTwoMall.getMeal2();
                break;
            case 3:
                meal = threeMall.getMeal();
                break;
            case 6:
                meal = sixAndSevenMall.getMeal6();
                break;
            case 7:
                meal = sixAndSevenMall.getMeal7();
                break;
            case 8:
                meal = backStreet.getMeal();
                break;
        }
        return meal;
    }

    public void ChooseMealAllDayRandom(){
        Ui ui = new Ui();
        List<Runnable> methods = new ArrayList<>();
        methods.add(ui::ChoosePie); // this is how you reference a method itself
        methods.add(ui::ChooseRice);
        methods.add(ui::ChooseNoodle);
        int randomNumber = new Random().nextInt(methods.size());
//        methods.get(randomNumber).run();

        for (int i = 0; i < 2; i++) {
            methods.get(randomNumber).run();
        }
    }

    public void ChoosePie(){
        System.out.println("今天的选择是：" + new Pie().getMeal());
    }

    public void ChooseRice(){
        System.out.println("今天的选择是：" + new Rice().getMeal());
    }

    public void ChooseNoodle(){
        System.out.println("今天的选择是：" + new Noodle().getMeal());
    }

}



